跳到主要内容

Jenkins 搭配 SonarQube 代码审查

TODO: 用到再更新吧...

SonaQube 简介

SonarQube 是一个用于管理代码质量的开放平台,可以快速的定位代码中潜在的或者明显的错误。目前支持 java,C#,C/C++,Python,PL/SQL,Cobol,JavaScrip,Groovy 等二十几种编程语言的代码质量管理与检测。

官网地址 https://www.sonarqube.org/

安装配置

安装 MySQL(旧版)

sudo apt-get install mysql-server

初始化 MySQL 配置

sudo mysql_secure_installation

配置项较多,如下所示

#1
VALIDATE PASSWORD PLUGIN can be used to test passwords...
Press y|Y for Yes, any other key for No: N (我的选项)

#2
Please set the password for root here...
New password: (输入密码)
Re-enter new password: (重复输入)

#3
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them...
Remove anonymous users? (Press y|Y for Yes, any other key for No) : N (我的选项)

#4
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network...
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y (我的选项)

#5
By default, MySQL comes with a database named 'test' that
anyone can access...
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N (我的选项)

#6
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y (我的选项)

检查 mysql 服务状态

systemctl status mysql.service

显示如下结果说明 mysql 服务是正常的:

在 MySQL 创建 sonar 数据库

# 先连接数据库
mysql -uroot -p

# 进入控制台后
mysql> create database sonar;
mysql> show databases;

安装 SonarQube(旧版)

FIXME: 注意,SonarQube7.9 后不支持 MySQL 了

到官网下载 SonarQube 的开源版 https://www.sonarqube.org/success-download-community-edition/

解压 sonar,并设置权限

sudo apt install unzip
unzip sonarqube-8.9.0.43852.zip # 解压
sudo mkdir /opt/sonar # 创建目录
sudo mv sonarqube-8.9.0.43852/* /opt/sonar # 移动文件
sudo useradd sonar # 创建sonar用户,必须sonar用于启动,否则报错
sudo passwd sonar # 设置初始密码 123456
sudo chown -R sonar. /opt/sonar # 更改sonar目录及文件权限

修改 sonar 配置文件

sudo vi /opt/sonar/conf/sonar.properties

内容如下:

sonar.jdbc.username=root 
sonar.jdbc.password=123456
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false

注意:sonar 默认监听 9000 端口,如果 9000 端口被占用,需要更改。

启动 sonar

cd /opt/sonar
su sonar ./bin/linux-x86-64/sonar.sh start # 启动
su sonar ./bin/linux-x86-64/sonar.sh status # 查看状态
su sonar ./bin/linux-x86-64/sonar.sh stop # 停止
tail -f ./logs/sonar.logs # 查看日志

访问 sonar

http://192.168.211.129:9000/

安装 PostgreSQL(新版)

注意:SonarQube 支持的数据库有 PostgreSQL,Microsoft SQL Server,Oracle,注意7.9版本已经不对 MySql 进行官方的支持了,因此可以直接跳到下

sudo apt-get -y install postgresql

一旦安装完成, PostgreSQL 服务将会自动启动。使用 psql 工具通过连接 PostgreSQL 数据库并且打印它的版本来验证安装:

sudo -u postgres psql -c "SELECT version();"

就这样。PostgreSQL 已经安装好了,你可以开始使用它了。

初始化数据库

service postgresql initdb

修改默认密码

为了以 postgres 用户身份登录 PostgreSQL 服务器,首先切换用户,然后使用 psql 工具访问 PostgreSQL。

sudo su - postgres
psql
alter user postgres with password '123456';

修改后重启服务

service postgresql restart

测试登陆

psql -h 127.0.0.1 -p 5432 -U postgres

创建数据库

postgres=# create database sonar;
# 查看所有数据库
postgres=# \l

配置 SonarQube

sudo vi /opt/sonar/conf/sonar.properties
sonar.jdbc.url=jdbc:postgresql://localhost/sonar
sonar.jdbc.username=root
sonar.jdbc.password=123456

再次启动测试

cd /opt/sonar
su sonar ./bin/linux-x86-64/sonar.sh start # 启动
su sonar ./bin/linux-x86-64/sonar.sh status # 查看状态
su sonar ./bin/linux-x86-64/sonar.sh stop # 停止
tail -f ./logs/sonar.logs # 查看日志

sonarqube web 默认端口号为 9000 如果想修改成其他端口可以通过下面这个设置修改其他端口号。

访问 sonar

http://192.168.211.129:9000/

TODO: 报错

2021.05.28 19:16:25 INFO  app[][o.s.a.SchedulerImpl] Waiting for Elasticsearch to be up and running
warning: no-jdk distributions that do not bundle a JDK are deprecated and will be removed in a future release
OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.
ERROR: [1] bootstrap checks failed. You must address the points described in the following [1] lines before starting Elasticsearch.
bootstrap check failure [1] of [1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
ERROR: Elasticsearch did not exit normally - check the logs at /opt/sonar/logs/sonarqube.log
2021.05.28 19:17:35 WARN app[][o.s.a.p.AbstractManagedProcess] Process exited with exit value [es]: 78
2021.05.28 19:17:35 INFO app[][o.s.a.SchedulerImpl] Process[es] is stopped
2021.05.28 19:17:35 INFO app[][o.s.a.SchedulerImpl] SonarQube is stopped

Reference

MySQL: SonarQube和Gitlab放弃支持的原因 SonarQube系列一、Linux安装与部署